package sixth.group.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sixth.group.dao.CustomerMapper;
import sixth.group.domain.Customer;
import sixth.group.domain.CustomerExample;
import sixth.group.service.CustomerService;


import java.util.List;

@Service(value = "customerservice")
public class CustomerServiceimpl implements CustomerService {

    @Autowired
    private CustomerMapper customerMapper;


    @Override
    public boolean register(Customer customer) {

        CustomerExample customerExample=new CustomerExample();
        CustomerExample.Criteria criteria1=customerExample.createCriteria();
        CustomerExample.Criteria criteria2=customerExample.createCriteria();
        criteria1.andCusernameEqualTo(customer.getCusername());
        criteria2.andCphoneEqualTo(customer.getCphone());
        customerExample.or(criteria2);

         List<Customer> customers=customerMapper.selectByExample(customerExample);

        if (customers.size() > 0){
            System.out.println("顾客注册失败：存在同名用户或手机号相同用户");
            return false;
        }else {
            //2 将信息插入到数据库中
            int i = customerMapper.insertSelective(customer);
            System.out.println("顾客注册成功：成功添加了" + i + "个顾客");
            return true;
        }
    }



    @Override
    public boolean loginCheck(Customer customer) {
        boolean flag = false;

        CustomerExample customerExample = new CustomerExample();
        CustomerExample.Criteria criteria = customerExample.createCriteria();
        criteria.andCusernameEqualTo(customer.getCusername()).andCpassEqualTo(customer.getCpass());
        List<Customer> customers = customerMapper.selectByExample(customerExample);
        if(customers.size() > 0){
            flag = true;
        }else{
            flag = false;
        }
        return flag;
    }

    @Override
    public boolean modify(Customer customer) {
        CustomerExample customerExample=new CustomerExample();
        CustomerExample.Criteria criteria1=customerExample.createCriteria();
        CustomerExample.Criteria criteria2=customerExample.createCriteria();
        criteria1.andCusernameEqualTo(customer.getCusername()).andCidNotEqualTo(customer.getCid());
        criteria2.andCphoneEqualTo(customer.getCphone()).andCidNotEqualTo(customer.getCid());
        customerExample.or(criteria2);

        List<Customer> customers=customerMapper.selectByExample(customerExample);

        if (customers.size() > 0) {
            System.out.println("顾客修改信息失败：存在同名用户或手机号相同用户");
            return false;
        }
        else customerMapper.updateByPrimaryKeySelective(customer);
        return true;
    }

    @Override
    public Customer findbyname(String name) {
        CustomerExample customerExample = new CustomerExample();
        CustomerExample.Criteria criteria = customerExample.createCriteria();
        criteria.andCusernameEqualTo(name);
        List<Customer> customers = customerMapper.selectByExample(customerExample);

        if(customers.size()>0)return customers.get(0);
        else return null;
    }

    @Override
    public void insertimage(Customer customer, String image) {
        Customer customerwithid=findbyname(customer.getCusername());
        customerwithid.setCimage(image);
        customerMapper.updateByPrimaryKeySelective(customerwithid);
    }
}
